import cgi
import datetime

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

class MyMsg(db.Model):
  name = db.StringProperty()  
  content = db.StringProperty(multiline=True)
  date = db.DateTimeProperty(auto_now_add=True)
  contact=db.StringProperty(multiline=True)
  isAuth=db.BooleanProperty()
  ip=db.StringProperty();

class MainPage(webapp.RequestHandler):
    def get(self):
        result="<result>"
        msgs = db.GqlQuery("SELECT * FROM MyMsg WHERE isAuth=:isAuth ORDER BY date DESC",isAuth=True)
        for msg in msgs:
            result+="<msg ip='"+str(msg.ip)+"'  name='"+msg.name+"' time='"+datetime.datetime.strftime(msg.date,"%Y-%m-%d %H:%M:%S")+"' content='"+msg.content+"'/>"
        result+="</result>"
        self.response.out.write(result)
    def post(self):
        msg=MyMsg();
        msg.name=self.request.get('name')
        msg.content=self.request.get('content')
        msg.contact=self.request.get('contact')
        msg.isAuth=False
        msg.ip=str(self.request.remote_addr)
        msg.put();
application = webapp.WSGIApplication(
                                     [('/msg', MainPage)],
                                     debug=True)

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()